﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatabaseAdminTools.Connections;

namespace DatabaseAdminTools.OptionForms
{
    public partial class GlobalPrivs : UserControl
    {
        private string[] globalPrivs = new String[] { "Select","Insert","Update","Delete","Create","Drop","Reload","Showdown",
            "Process","File","Grant","References","Index","Alter","Show Databases","Super","Create Tmp. Tables","Lock Tables",
            "Execute","Replication Slave","Replication Client","Create View","Show View","Create Routine","Alter Routine","Create User","Event","Trigger"};

        //private string[] databasePrivs = new String[] { "Select","Insert","Update","Delete","Create","Drop","Grant","References","Index","Alter","Create Tmp. Tables","Lock Tables",
        //    "Create View","Show View","Create Routine","Alter Routine","Create User","Event","Trigger"};

        //private string[] tablePrivs = new String[] { "Select","Insert","Update","Delete","Create","Drop","Grant","References","Index","Alter","Create View","Show View"};

        //private string[] tcolumnPrivs = new String[] { "Select", "Insert", "Update", "References" };

        private CheckBox[] checkBox;
        private User user;

        public GlobalPrivs()
        {
            InitializeComponent();
            saveButton.Enabled = false;
        }

        public void setData(User _user, int level)
        {
            user = _user;
            switch (level)
            {
                case 0: // global
                    try
                    {
                        user.setData();
                        panel1.Controls.Clear();
                        toolStripLabel1.Text = user.ToString();
                        checkBox = new CheckBox[28];
                        for (int i = 0; i < 28; i++)
                        {
                            checkBox[i] = new CheckBox();
                            checkBox[i].Text = globalPrivs[i];
                            checkBox[i].Checked = user.Privileges[i];
                            panel1.Controls.Add(checkBox[i]);
                            if (i < 10)
                            {
                                checkBox[i].Location = new Point(3, 23 * i);
                            }
                            else if (i < 20)
                            {
                                checkBox[i].Location = new Point(150, 23 * (i - 10));
                            }
                            else
                            {
                                checkBox[i].Location = new Point(300, 23 * (i - 20));
                            }
                            checkBox[i].CheckedChanged += new EventHandler(GlobalPrivs_CheckedChanged);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    break;
                default: break;
            }
        }

        private bool isSaved()
        {
            for (int i = 0; i < 28; i++)
            {
                if (checkBox[i].Checked != user.Privileges[i])
                    return false;
            }
            return true;
        }

        private void GlobalPrivs_CheckedChanged(object sender, EventArgs e)
        {
            saveButton.Enabled = !isSaved();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkBox.Length; i++)
            {
                checkBox[i].Checked = true;
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkBox.Length; i++)
            {
                checkBox[i].Checked = false;
            }
        }

        public void save()
        {
            if (saveButton.Enabled)
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    for (int i = 0; i < checkBox.Length; i++)
                    {
                        user.Privileges[i] = checkBox[i].Checked;
                    }
                    user.updatePrivileges();
                    saveButton.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                this.Cursor = Cursors.Default;
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            save();
        }
    }
}
